package spider;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.Collections;
import java.util.*;

public class LocalRes {

    public static int count=0;
    /**
     * @author stone
     * @date 2021/07/24 下午 07:05
     * @params [String]
     * @return List<String>
     * @desc: 获取目标页面中的符合条件的其他页面
     */
    public static List<String> getUrl2(String baseUrl) throws IOException {
        /*
          主页中的a标签的href属性值有以下几种类型 我们需要类型3 类型4 类型10
           1  #
           2  javascript:void(0);
           3  job/Index.aspx
           4  zhaopin/11324.html
           5  info/public.aspx?classid=1050
           6  http://brqw.hrfwy.com/
   ~       7  http://www.hengyang.gov.cn/hysrsj/
           8  http://
           9  jobseek/key.aspx?class=C
           10 http://www.hysrlzy.com/News/974.html
         */
        List<String> list = new ArrayList<>();
        Document document = Jsoup.parse(new URL(baseUrl), 5000);
        Elements select = document.select("a");
        for (int i = 0; i < select.size(); i++) {
            String href = select.get(i).attr("href");
            if(href.endsWith(".aspx") || href.endsWith(".html") ){
                //类型10需要去掉根路径
                if(href.contains(baseUrl)){
                    href = href.replaceAll(baseUrl,"");
                }
                if(list.contains(href)==false){
                    list.add(href);
                }
            }
        }
        return list;
    }

    public static void local2(String url,String filePath) throws IOException {

        String baseUrl = "http://www.hysrlzy.com/";
        if(url.contains("http")==false){
            filePath += url;
            url = baseUrl+url;
        }
        count++;
        System.out.println("下载 "+count+":"+url);
        Document document = null;
        document = Jsoup.parse(new URL(url), 5000);

        File file = new File(filePath);
        File dir = new File(file.getParentFile().getAbsolutePath());
        if(!dir.exists()){
            dir.mkdirs();
        }
        //获得目标资源
        getRes(file,"js",document,url);
        getRes(file,"css",document,url);
        getRes(file,"img",document,url);

        System.out.println("写入的文件名"+file.getName());
        try (FileWriter fw = new FileWriter(file)){
            fw.write(document.toString());
        }
        System.out.println();

    }

    /**
     * @author stone
     * @date 2021/07/24 下午 07:12
     * @params [File, String, Document, String]
     * @return void
     * @desc: 根据目标资源类型获取相应的资源
     */
    public static void getRes(File file,String resType, Document document, String baseUrl) throws IOException {

        String selectName = ""; //要选择的标签名
        String attrName = "";   //标签属性名
        String endName = "";    //文件后缀

        //根据资源类型获取初始化相应的变量
        if(resType.equals("css")){
            selectName = "link";
            attrName = "href";
            endName = "css";
        } else if(resType.equals("js")) {
            selectName = "script";
            attrName = "src";
            endName = "js";
        } else if (resType.equals("img")){
            selectName = "img";
            attrName = "src";
            endName = "bmp,jpg,png,tif,gif,...";
        } else {
            // 获取其他资源类型....
        }
        //获取目标资源
        Elements select = document.select(selectName);
        for (int i=0;i<select.size();i++){
            //这里的遇到没有要匹配的attr属性直接跳过  ||  获得文件的后缀  判断是否与目标匹配后缀
            String webpath = select.get(i).attr(attrName);
            String fileTail = webpath.substring(webpath.lastIndexOf(".")+1);
            if(webpath.equals("") || endName.contains(fileTail) == false) continue;

            //遇到相对路径
            if(webpath.startsWith("../")){  //上一级目录
                webpath = baseUrl.substring(0,baseUrl.lastIndexOf("/")+1)+webpath;
            }else if(webpath.startsWith("/")){  //根目录
                webpath = "http://www.hysrlzy.com"+webpath;
            }else if(webpath.startsWith("./")){
                webpath = baseUrl.substring(0,baseUrl.lastIndexOf("/")+1)+webpath;
            }else {
                webpath = baseUrl+webpath;
            }

            // 如果是其他远程的地址,不用修改路径,不是就修改地址
            if(webpath.startsWith("http")==false){
                webpath = baseUrl + webpath;
            }

            //将资源的文件路径转换为本地路径
            String diskPath = file.getParentFile().getAbsolutePath();
            diskPath += "/"+ resType;
            File dir = new File(diskPath);
            if(dir.exists()==false){
                dir.mkdirs();
            }
            //获得新的文件的名字
            String newFileName = webpath.substring(webpath.lastIndexOf("/")+1);
            File newFile = new File(dir,newFileName);
            //下载资源
            download(webpath,newFile);
            //修改属性
            select.get(i).attr(attrName,resType+"/"+newFile.getName());
        }
    }



    /**
     * 页面本地化,
     * css 文件保存到 file 的当前目录下css 目录,
     * js 文件保存到 file 的当前目录下js 目录,
     * 图片 文件保存到 file 的当前目录下images 目录
     *
     * @param baseUrl
     * @param filepath
     */
    public  static void loacal(String baseUrl, String filepath) throws IOException {
        // 保存 html
        Document doc = Jsoup.parse(new URL(baseUrl), 5000);
        File file = new File(filepath);

        // 读取所有的 css, js, 图片元素
        Elements css = doc.select("link");
        for (int i = 0; i < css.size(); i++) {
            // 提取文件保存本地目录
            String webpath = css.get(i).attr("href");
            if(webpath.endsWith(".css") == false){
                continue;
            }
            webpath = baseUrl + webpath;
            String diskpath = file.getParentFile().getAbsolutePath();
            diskpath += "/css";
            File cssDir = new File(diskpath);
            if(cssDir.exists() == false){
                cssDir.mkdirs();
            }
            // 获取文件名
            String cssName = webpath.substring(webpath.lastIndexOf("/")+1);
            File cssFile = new File( cssDir, cssName);
            download(webpath, cssFile);

            // 替换原有路径
            css.get(i).attr("href","css/" + cssFile.getName());

        }

        Elements js = doc.select("script");
        for (int i = 0; i < js.size(); i++) {
            // 提取文件保存本地目录
            String webpath = js.get(i).attr("src");
            if(webpath.endsWith(".js") == false){
                continue;
            }
            webpath = baseUrl + webpath;
            String diskpath = file.getParentFile().getAbsolutePath();
            diskpath += "/js";
            File cssDir = new File(diskpath);
            if(cssDir.exists() == false){
                cssDir.mkdirs();
            }
            // 获取文件名
            String cssName = webpath.substring(webpath.lastIndexOf("/")+1);
            File cssFile = new File( cssDir, cssName);
            download(webpath, cssFile);

            // 替换原有路径
            js.get(i).attr("src","js/" + cssFile.getName());

        }

        try (FileWriter fw = new FileWriter(file)) {
            fw.write(doc.toString());
        }
    }

    /**
     * 下载文件
     * @param url
     * @param file
     */
    private static void download(String url, File file) throws IOException {
        URL u = new URL(url);
        URLConnection c = u.openConnection();
        try( InputStream in = c.getInputStream();
             FileOutputStream fos = new FileOutputStream(file)){
            byte[] buffer = new byte[1024];
            int count;
            while ((count = in.read(buffer)) > 0){
                fos.write(buffer,0,count);
            }
        }
    }

}
